//
//  MBProgressHUD+Helper.m
//  智能备考学习
//
//  Created by 盛超 on 2018/8/14.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "MBProgressHUD+Helper.h"

@implementation MBProgressHUD (Helper)

+ (instancetype)showDefaultToView:(UIView *)view {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabel.text = @"加载中...";
    
    return hud;
}

+ (instancetype)showClearStyleToView:(UIView *)view {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.color = [UIColor colorWithWhite:0.f alpha:0.f];
    
    return hud;
}

+(instancetype)showErrorStyleToView:(UIView *)view withCode:(NSInteger)code{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    hud.label.text =[NSString stringWithFormat:@"错误代码:%ld",code];
    hud.label.textColor = [UIColor whiteColor];
    hud.label.font = [UIFont systemFontOfSize:15.0f];
    [hud hideAnimated:YES afterDelay:1.0f];

    return hud;
}


+ (instancetype)showNoDataStyleToView:(UIView *)view {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    hud.offset = CGPointMake(0, [UIScreen mainScreen].bounds.size.height/2-80);
    hud.label.text = @"没有更多内容";
    hud.label.textColor = [UIColor whiteColor];
    hud.label.font = [UIFont systemFontOfSize:15.0f];
    [hud hideAnimated:YES afterDelay:1.0f];
    
    return hud;
}

+ (instancetype)showCustomAnimationToView:(UIView*)view images:(NSArray *)images {
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.animationImages = images;
    imageView.animationDuration = 0.1;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:imageView animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor clearColor];
    
    return hud;
}




#pragma mark - ********************* kz提示框 *********************
+ (UIWindow *)keywindow {
    
    return [UIApplication sharedApplication].keyWindow;
}
static CGFloat kDefaultDuration = 2.0f;

+ (void)hudDefaultConfig:(MBProgressHUD *)hud {
    
    // 设置控件的颜色
    hud.contentColor = [UIColor colorWithWhite:1 alpha:1];
    // 设置mask背景
    hud.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
    // 设置text的颜色
    hud.label.font = [UIFont systemFontOfSize:14];
    hud.label.numberOfLines = 0;
    // 设置detailText的颜色
    hud.detailsLabel.font = [UIFont systemFontOfSize:12];
    // 设置hud背景框的颜色
    hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.8];
    // 设置hud隐藏自动移除功能
    hud.removeFromSuperViewOnHide = YES;
    // 设置hud显示和隐藏动画
    hud.animationType = MBProgressHUDAnimationZoom;
}
/*
 tips:
 1.默认显示时长为2秒
 */
// 仅显示文字
+ (void)showOnlyText:(NSString *)text {
    
    [self showOnlyText:text hasMasker:NO];
}
+ (void)showOnlyText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showOnlyText:text position:KZHUDPositionModeCenter];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}
+ (void)showOnlyText:(NSString *)text position:(KZHUDPositionMode)position {
    
    [self showOnlyText:text position:position duration:kDefaultDuration];
}
+ (void)showOnlyText:(NSString *)text position:(KZHUDPositionMode)position duration:(CGFloat)duration {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeText;
    // 设置文本
    hud.label.text = text;
    // 设置位置
    CGFloat offsetY = 0;
    switch (position) {
        case KZHUDPositionModeTop:
            offsetY = -MBProgressMaxOffset;
            break;
        case KZHUDPositionModeCenter:
            offsetY = 0;
            break;
        case KZHUDPositionModeBottom:
            offsetY = MBProgressMaxOffset;
            break;
        default:
            break;
    }
    hud.offset = CGPointMake(0.f, offsetY);
    
    [hud hideAnimated:YES afterDelay:duration];
}



// 加载中
+ (void)showLoadind:(NSString *)text {
    
    [self showLoadind:text hasMasker:NO];
}
+ (void)showLoadind:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置有否点击
    hud.userInteractionEnabled = hasMasker;
    // 设置背景色
    hud.backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
}
+ (void)showLoadind:(NSString *)text duration:(CGFloat)duration {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    
    [hud hideAnimated:YES afterDelay:duration];
}




// 成功/失败
+ (void)showText:(NSString *)text state:(KZHUDStateMode)state {
    
    [self showText:text state:state duration:kDefaultDuration];
}
+ (void)showText:(NSString *)text state:(KZHUDStateMode)state duration:(CGFloat)duration {
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 设置自定义图片
    UIImage *image = nil;
    switch (state) {
        case KZHUDStateModeSuccess:
            image = [UIImage imageNamed:@"alert_success"];
            break;
        case KZHUDStateModeFailure:
            image = [UIImage imageNamed:@"alert_failure"];
            break;
        default:
            break;
    }
    hud.customView = [[UIImageView alloc] initWithImage:image];
    hud.square = NO;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    [hud hideAnimated:YES afterDelay:kDefaultDuration];
}
+ (void)showFailureWithText:(NSString *)text {
    
    [self showFailureWithText:text hasMasker:NO];
}
+ (void)showFailureWithText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showText:text state:KZHUDStateModeFailure];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}
+ (void)showSuccessWithText:(NSString *)text {
    
    [self showSuccessWithText:text hasMasker:NO];
}
+ (void)showSuccessWithText:(NSString *)text hasMasker:(BOOL)hasMasker {
    
    [self showText:text state:KZHUDStateModeSuccess];
    [MBProgressHUD HUDForView:[self keywindow]].userInteractionEnabled = hasMasker;
    [MBProgressHUD HUDForView:[self keywindow]].backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
}

// 加载进度
+ (MBProgressHUD *)showProgress:(NSString *)text action:(void(^)(KZHUDProgressUpload progress))actionBlock {
    
    return [self showProgress:text action:actionBlock hasMasker:NO];
}

+ (MBProgressHUD *)showProgress:(NSString *)text action:(void(^)(KZHUDProgressUpload progress))actionBlock hasMasker:(BOOL)hasMasker {
    
    [MBProgressHUD hideHUDForView:[self keywindow] animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[self keywindow] animated:YES];
    // 设置基本属性
    [self hudDefaultConfig:hud];
    // 设置有否点击
    hud.userInteractionEnabled = hasMasker;
    hud.backgroundView.backgroundColor = hasMasker ? [UIColor colorWithWhite:0 alpha:0.2] : [UIColor clearColor];
    // 设置模式
    hud.mode = MBProgressHUDModeDeterminateHorizontalBar;
    // 设置文本
    if (text) {
        hud.label.text = text;
    }
    
    if (actionBlock) {
        
        KZHUDProgressUpload progressBlock = ^ (CGFloat progress, BOOL competion) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD HUDForView:[self keywindow]].progress = progress;
            });
            
            if (competion) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                });
            }
        };
        actionBlock(progressBlock);
    }
    
    
    return hud;
}

// 隐藏
+ (void)hideHud {
    
    [[MBProgressHUD HUDForView:[self keywindow]] hideAnimated:YES];
}


@end
